package com.jiyi.activity.datainput.sm;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jiyi.bean.datainput.new_input.BaseCheck;
import com.jiyi.bean.datainput.new_input.FireManageBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.NoScrollGridView;
import com.jiyi.widget.picselect.PicsAdapter;
import com.jiyi.widget.picshow.ImagePagerActivity;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.RadioGroupUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/29.
 */

public class BuildSpaceManageReadActivity extends ActivityBase {
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.rbDutyYes)
    RadioButton rbDutyYes;
    @BindView(R.id.rbDutyNo)
    RadioButton rbDutyNo;
    @BindView(R.id.rgDutyIsOK)
    RadioGroup rgDutyIsOK;
    @BindView(R.id.tvDutyProblem)
    TextView tvDutyProblem;
    @BindView(R.id.tvDutyMeasure)
    TextView tvDutyMeasure;
    @BindView(R.id.rbEachDutyYes)
    RadioButton rbEachDutyYes;
    @BindView(R.id.rbEachDutyNo)
    RadioButton rbEachDutyNo;
    @BindView(R.id.rgEachDutyIsOK)
    RadioGroup rgEachDutyIsOK;
    @BindView(R.id.tvEachDutyProblem)
    TextView tvEachDutyProblem;
    @BindView(R.id.tvEachDutyMeasure)
    TextView tvEachDutyMeasure;
    @BindView(R.id.rbSafeCheckYes)
    RadioButton rbSafeCheckYes;
    @BindView(R.id.rbSafeCheckNo)
    RadioButton rbSafeCheckNo;
    @BindView(R.id.rgSafeCheckIsOK)
    RadioGroup rgSafeCheckIsOK;
    @BindView(R.id.tvSafeCheckProblem)
    TextView tvSafeCheckProblem;
    @BindView(R.id.tvSafeCheckMeasure)
    TextView tvSafeCheckMeasure;
    @BindView(R.id.rbDoorIsViolationYes)
    RadioButton rbDoorIsViolationYes;
    @BindView(R.id.rbDoorIsViolationNo)
    RadioButton rbDoorIsViolationNo;
    @BindView(R.id.rgDoorIsViolation)
    RadioGroup rgDoorIsViolation;
    @BindView(R.id.tvDoorIsViolationProblem)
    TextView tvDoorIsViolationProblem;
    @BindView(R.id.tvDoorIsViolationMeasure)
    TextView tvDoorIsViolationMeasure;
    @BindView(R.id.rbCertificateYes)
    RadioButton rbCertificateYes;
    @BindView(R.id.rbCertificateNo)
    RadioButton rbCertificateNo;
    @BindView(R.id.rgHasCertificate)
    RadioGroup rgHasCertificate;
    @BindView(R.id.tvCertificateProblem)
    TextView tvCertificateProblem;
    @BindView(R.id.tvCertificateMeasure)
    TextView tvCertificateMeasure;
    @BindView(R.id.rbTrainYes)
    RadioButton rbTrainYes;
    @BindView(R.id.rbTrainNo)
    RadioButton rbTrainNo;
    @BindView(R.id.rgHasTrain)
    RadioGroup rgHasTrain;
    @BindView(R.id.tvTrainProblem)
    TextView tvTrainProblem;
    @BindView(R.id.tvTrainMeasure)
    TextView tvTrainMeasure;
    @BindView(R.id.rbFireStationYes)
    RadioButton rbFireStationYes;
    @BindView(R.id.rbFireStationNo)
    RadioButton rbFireStationNo;
    @BindView(R.id.rgHasFireStation)
    RadioGroup rgHasFireStation;
    @BindView(R.id.tvFireStationProblem)
    TextView tvFireStationProblem;
    @BindView(R.id.tvFireStationMeasure)
    TextView tvFireStationMeasure;
    @BindView(R.id.rbManageYes)
    RadioButton rbManageYes;
    @BindView(R.id.rbManageNo)
    RadioButton rbManageNo;
    @BindView(R.id.rgHasManage)
    RadioGroup rgHasManage;
    @BindView(R.id.tvManageProblem)
    TextView tvManageProblem;
    @BindView(R.id.tvManageMeasure)
    TextView tvManageMeasure;
    @BindView(R.id.rbMaintenanceUnitYes)
    RadioButton rbMaintenanceUnitYes;
    @BindView(R.id.rbMaintenanceUnitNo)
    RadioButton rbMaintenanceUnitNo;
    @BindView(R.id.rbMaintenanceUnitNoInvolving)
    RadioButton rbMaintenanceUnitNoInvolving;
    @BindView(R.id.rgHasMaintenanceUnit)
    RadioGroup rgHasMaintenanceUnit;
    @BindView(R.id.tvMaintenanceUnit)
    TextView tvMaintenanceUnit;
    @BindView(R.id.tvMaintenanceUnitProblem)
    TextView tvMaintenanceUnitProblem;
    @BindView(R.id.tvMaintenanceUnitMeasure)
    TextView tvMaintenanceUnitMeasure;
    @BindView(R.id.rbDailyMaintenanceYes)
    RadioButton rbDailyMaintenanceYes;
    @BindView(R.id.rbDailyMaintenanceNo)
    RadioButton rbDailyMaintenanceNo;
    @BindView(R.id.rgHasDailyMaintenance)
    RadioGroup rgHasDailyMaintenance;
    @BindView(R.id.tvDailyMaintenanceProblem)
    TextView tvDailyMaintenanceProblem;
    @BindView(R.id.tvDailyMaintenanceMeasure)
    TextView tvDailyMaintenanceMeasure;
    @BindView(R.id.rbGuardianshipYes)
    RadioButton rbGuardianshipYes;
    @BindView(R.id.rbGuardianshipNo)
    RadioButton rbGuardianshipNo;
    @BindView(R.id.rgHasGuardianship)
    RadioGroup rgHasGuardianship;
    @BindView(R.id.tvGuardianshipProblem)
    TextView tvGuardianshipProblem;
    @BindView(R.id.tvGuardianshipMeasure)
    TextView tvGuardianshipMeasure;
    @BindView(R.id.rbRometeManageYes)
    RadioButton rbRometeManageYes;
    @BindView(R.id.rbRometeManageNo)
    RadioButton rbRometeManageNo;
    @BindView(R.id.rgHasRometeManage)
    RadioGroup rgHasRometeManage;
    @BindView(R.id.tvRometeManageProblem)
    TextView tvRometeManageProblem;
    @BindView(R.id.tvRometeManageMeasure)
    TextView tvRometeManageMeasure;

    String build_id;
    @BindView(R.id.gvDuty)
    NoScrollGridView gvDuty;
    @BindView(R.id.gvEachDuty)
    NoScrollGridView gvEachDuty;
    @BindView(R.id.gvSafeCheck)
    NoScrollGridView gvSafeCheck;
    @BindView(R.id.gvDoorIsViolation)
    NoScrollGridView gvDoorIsViolation;
    @BindView(R.id.gvCertificate)
    NoScrollGridView gvCertificate;
    @BindView(R.id.gvTrain)
    NoScrollGridView gvTrain;
    @BindView(R.id.gvFireStation)
    NoScrollGridView gvFireStation;
    @BindView(R.id.gvManage)
    NoScrollGridView gvManage;
    @BindView(R.id.gvMaintenanceUnit)
    NoScrollGridView gvMaintenanceUnit;
    @BindView(R.id.gvDailyMaintenance)
    NoScrollGridView gvDailyMaintenance;
    @BindView(R.id.gvGuardianship)
    NoScrollGridView gvGuardianship;
    @BindView(R.id.gvRometeManage)
    NoScrollGridView gvRometeManage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_build_space_manage_read);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        getIntentData();
        setTop();
        setViewStatus();

        if (!TextUtils.isEmpty(build_id)) {
            //修改时，获取数据
            getData();
        }
    }

    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("消防安全管理方面");
        tvSearch.setVisibility(View.GONE);
    }

    private void setViewStatus() {
        RadioGroupUtil.disableRadioGroup(rgDoorIsViolation);
        RadioGroupUtil.disableRadioGroup(rgDutyIsOK);
        RadioGroupUtil.disableRadioGroup(rgEachDutyIsOK);
        RadioGroupUtil.disableRadioGroup(rgHasCertificate);
        RadioGroupUtil.disableRadioGroup(rgHasDailyMaintenance);
        RadioGroupUtil.disableRadioGroup(rgHasFireStation);
        RadioGroupUtil.disableRadioGroup(rgHasGuardianship);
        RadioGroupUtil.disableRadioGroup(rgHasMaintenanceUnit);
        RadioGroupUtil.disableRadioGroup(rgHasManage);
        RadioGroupUtil.disableRadioGroup(rgHasRometeManage);
        RadioGroupUtil.disableRadioGroup(rgHasTrain);
        RadioGroupUtil.disableRadioGroup(rgSafeCheckIsOK);
    }

    private void getData() {
        HttpMethods.getInstance().getFireManageInfo(UserInfoUtil.getCurrentUserAccessToken(), build_id, 2)
                .subscribe(new SampleProgressObserver<FireManageBean>(this) {
                    @Override
                    public void onNext(FireManageBean bean) {

                        if (bean == null || "".equals(bean.getGson_null())) {
                            bean = new FireManageBean();
                            BaseCheck bc1 = new BaseCheck();
                            bc1.setIs_ok(0);
                            bc1.setProblem("");
                            bc1.setMeasures("");
                            bean.setResponse_is_implemented(bc1);
                            BaseCheck bc2 = new BaseCheck();
                            bc2.setIs_ok(0);
                            bc2.setProblem("");
                            bc2.setMeasures("");
                            bean.setSafety_response_clearly(bc2);
                            BaseCheck bc3 = new BaseCheck();
                            bc3.setIs_ok(0);
                            bc3.setProblem("");
                            bc3.setMeasures("");
                            bean.setUnit_safety_check(bc3);
                            BaseCheck bc4 = new BaseCheck();
                            bc4.setIs_ok(0);
                            bc4.setProblem("");
                            bc4.setMeasures("");
                            bean.setPipe_well_occupation(bc4);
                            BaseCheck bc5 = new BaseCheck();
                            bc5.setIs_ok(0);
                            bc5.setProblem("");
                            bc5.setMeasures("");
                            bean.setElectrical_wiring_equipment(bc5);
                            BaseCheck bc6 = new BaseCheck();
                            bc6.setIs_ok(0);
                            bc6.setProblem("");
                            bc6.setMeasures("");
                            bean.setViolate_rules_rescue(bc6);
                            BaseCheck bc7 = new BaseCheck();
                            bc7.setIs_ok(0);
                            bc7.setProblem("");
                            bc7.setMeasures("");
                            bean.setControl_personnel_certificate(bc7);
                            BaseCheck bc8 = new BaseCheck();
                            bc8.setIs_ok(0);
                            bc8.setProblem("");
                            bc8.setMeasures("");
                            bean.setSafety_training_drill(bc8);
                            BaseCheck bc9 = new BaseCheck();
                            bc9.setIs_ok(1);
                            bc9.setProblem("");
                            bc9.setMeasures("");
                            bean.setBuilds_fire_station(bc9);
                            BaseCheck bc10 = new BaseCheck();
                            bc10.setIs_ok(0);
                            bc10.setProblem("");
                            bc10.setMeasures("");
                            bean.setElectrical_gas_maintenance(bc10);
                            BaseCheck bc11 = new BaseCheck();
                            bc11.setIs_ok(0);
                            bc11.setProblem("");
                            bc11.setMeasures("");
                            bean.setImplement_monitoring_safety(bc11);
                            BaseCheck bc12 = new BaseCheck();
                            bc12.setIs_ok(0);
                            bc12.setProblem("");
                            bc12.setMeasures("");
                            bean.setCombine_local_smart(bc12);
                            bean.setWb_com("");
                        }
                        fillData(bean);
                    }
                });
    }

    private void fillData(FireManageBean bean) {
        if (bean.getResponse_is_implemented().getIs_ok() == 1) {
            rbDutyYes.setChecked(true);
        }

        tvDutyProblem.setText(bean.getResponse_is_implemented().getProblem());
        tvDutyMeasure.setText(bean.getResponse_is_implemented().getMeasures());

        if (bean.getSafety_response_clearly().getIs_ok() == 1) {
            rbEachDutyYes.setChecked(true);
        }

        tvEachDutyProblem.setText(bean.getSafety_response_clearly().getProblem());
        tvEachDutyMeasure.setText(bean.getSafety_response_clearly().getMeasures());

        if (bean.getUnit_safety_check().getIs_ok() == 1) {
            rbSafeCheckYes.setChecked(true);
        }

        tvSafeCheckProblem.setText(bean.getUnit_safety_check().getProblem());
        tvSafeCheckMeasure.setText(bean.getUnit_safety_check().getMeasures());

        if (bean.getPipe_well_occupation().getIs_ok() == 1) {
            rbDoorIsViolationYes.setChecked(true);
        }

        tvDoorIsViolationProblem.setText(bean.getPipe_well_occupation().getProblem());
        tvDoorIsViolationMeasure.setText(bean.getPipe_well_occupation().getMeasures());

        if (bean.getElectrical_wiring_equipment().getIs_ok() == 1) {
            rbCertificateYes.setChecked(true);
        }

        tvCertificateProblem.setText(bean.getElectrical_wiring_equipment().getProblem());
        tvCertificateMeasure.setText(bean.getElectrical_wiring_equipment().getMeasures());

        if (bean.getViolate_rules_rescue().getIs_ok() == 1) {
            rbTrainYes.setChecked(true);
        }

        tvTrainProblem.setText(bean.getViolate_rules_rescue().getProblem());
        tvTrainMeasure.setText(bean.getViolate_rules_rescue().getMeasures());

        if (bean.getControl_personnel_certificate().getIs_ok() == 1) {
            rbFireStationYes.setChecked(true);
        }

        tvFireStationProblem.setText(bean.getControl_personnel_certificate().getProblem());
        tvFireStationMeasure.setText(bean.getControl_personnel_certificate().getMeasures());

        if (bean.getSafety_training_drill().getIs_ok() == 1) {
            rbManageYes.setChecked(true);
        }

        tvManageProblem.setText(bean.getSafety_training_drill().getProblem());
        tvManageMeasure.setText(bean.getSafety_training_drill().getMeasures());

        if (bean.getBuilds_fire_station().getIs_ok() == 1) {
            rbMaintenanceUnitYes.setChecked(true);
        } else if (bean.getBuilds_fire_station().getIs_ok() == 2) {
            rbMaintenanceUnitNo.setChecked(true);
        } else {
            rbMaintenanceUnitNoInvolving.setChecked(true);
        }
        tvMaintenanceUnit.setText(bean.getWb_com());
        tvMaintenanceUnitProblem.setText(bean.getBuilds_fire_station().getProblem());
        tvMaintenanceUnitMeasure.setText(bean.getBuilds_fire_station().getMeasures());

        if (bean.getElectrical_gas_maintenance().getIs_ok() == 1) {
            rbDailyMaintenanceYes.setChecked(true);
        }

        tvDailyMaintenanceProblem.setText(bean.getElectrical_gas_maintenance().getProblem());
        tvDailyMaintenanceMeasure.setText(bean.getElectrical_gas_maintenance().getMeasures());

        if (bean.getImplement_monitoring_safety().getIs_ok() == 1) {
            rbGuardianshipYes.setChecked(true);
        }

        tvGuardianshipProblem.setText(bean.getImplement_monitoring_safety().getProblem());
        tvGuardianshipMeasure.setText(bean.getImplement_monitoring_safety().getMeasures());

        if (bean.getCombine_local_smart().getIs_ok() == 1) {
            rbRometeManageYes.setChecked(true);
        }

        tvRometeManageProblem.setText(bean.getCombine_local_smart().getProblem());
        tvRometeManageMeasure.setText(bean.getCombine_local_smart().getMeasures());

        initGridViewList(bean);
    }

    void initGridViewList(FireManageBean fireManageBean)
    {
        if (fireManageBean.getResponse_is_implemented().getImage() == null || fireManageBean.getResponse_is_implemented().getImage().size() <= 0) {
            gvDuty.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvDuty,fireManageBean.getResponse_is_implemented().getImage());
        }

        if (fireManageBean.getSafety_response_clearly().getImage() == null || fireManageBean.getSafety_response_clearly().getImage().size() <= 0) {
            gvEachDuty.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvEachDuty,fireManageBean.getSafety_response_clearly().getImage());
        }

        if (fireManageBean.getUnit_safety_check().getImage() == null || fireManageBean.getUnit_safety_check().getImage().size() <= 0) {
            gvSafeCheck.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvSafeCheck,fireManageBean.getUnit_safety_check().getImage());
        }

        if (fireManageBean.getPipe_well_occupation().getImage() == null || fireManageBean.getPipe_well_occupation().getImage().size() <= 0) {
            gvDoorIsViolation.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvDoorIsViolation,fireManageBean.getPipe_well_occupation().getImage());
        }

        if (fireManageBean.getElectrical_wiring_equipment().getImage() == null || fireManageBean.getElectrical_wiring_equipment().getImage().size() <= 0) {
            gvCertificate.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvCertificate,fireManageBean.getElectrical_wiring_equipment().getImage());
        }

        if (fireManageBean.getViolate_rules_rescue().getImage() == null || fireManageBean.getViolate_rules_rescue().getImage().size() <= 0) {
            gvTrain.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvTrain,fireManageBean.getViolate_rules_rescue().getImage());
        }

        if (fireManageBean.getControl_personnel_certificate().getImage() == null || fireManageBean.getControl_personnel_certificate().getImage().size() <= 0) {
            gvFireStation.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvFireStation,fireManageBean.getControl_personnel_certificate().getImage());
        }

        if (fireManageBean.getSafety_training_drill().getImage() == null || fireManageBean.getSafety_training_drill().getImage().size() <= 0) {
            gvManage.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvManage,fireManageBean.getSafety_training_drill().getImage());
        }

        if (fireManageBean.getBuilds_fire_station().getImage() == null || fireManageBean.getBuilds_fire_station().getImage().size() <= 0) {
            gvMaintenanceUnit.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvMaintenanceUnit,fireManageBean.getBuilds_fire_station().getImage());
        }

        if (fireManageBean.getElectrical_gas_maintenance().getImage() == null || fireManageBean.getElectrical_gas_maintenance().getImage().size() <= 0) {
            gvDailyMaintenance.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvDailyMaintenance,fireManageBean.getElectrical_gas_maintenance().getImage());
        }

        if (fireManageBean.getImplement_monitoring_safety().getImage() == null || fireManageBean.getImplement_monitoring_safety().getImage().size() <= 0) {
            gvGuardianship.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvGuardianship,fireManageBean.getImplement_monitoring_safety().getImage());
        }

        if (fireManageBean.getCombine_local_smart().getImage() == null || fireManageBean.getCombine_local_smart().getImage().size() <= 0) {
            gvRometeManage.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvRometeManage,fireManageBean.getCombine_local_smart().getImage());
        }

    }

    void setGridviewAdapter(GridView gridView, List<String> picList)
    {
        gridView.setVisibility(View.VISIBLE);
        //图片列表
        if (picList != null) {
            final String [] urls = new String [picList.size()];
            for (int i = 0; i < picList.size(); i++) {
                urls[i] = picList.get(i);
            }

            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(view.getContext(),ImagePagerActivity.class);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                    view.getContext().startActivity(intent);
                }
            });
            PicsAdapter picsAdapter = new PicsAdapter(gridView.getContext(),picList);
            gridView.setAdapter(picsAdapter);
        }
    }

    @OnClick({R.id.tvBack})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            default:
                break;
        }
    }
}
